package com.example.proxy1;

import com.example.aspect.MyAspect;
import com.example.dao.UserDao;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

//jdk动态代理必须实现InvocationHandler接口
public class UserDaoProxy1 implements InvocationHandler {
    private UserDao userDao;//被代理增强目标对象
    private MyAspect aspect;//切面对象

    public UserDaoProxy1(UserDao userDao, MyAspect aspect) {
        this.userDao = userDao;
        this.aspect = aspect;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        aspect.checkPermissions();//检测权限
        Object result = method.invoke(userDao, args);//调用目标对象的方法
        aspect.log();//记录日志
        return result;
    }
}
